<?php
declare(strict_types=1);

namespace Modules\Admin\Service;

use HiCommon\Helper;
use HiCommon\Repository\NoticeRepository;
class NoticeService
{

    /**
     *  公告列表
     *  hinq 2019 08 21
     *
     **/
    public function get_list( array $arr_search ){
        $notice_types = ['1'=>'小程序','2'=>'安卓','3'=>'ios','4'=>'公众号','5'=>'后台'];
        $arr_list = NoticeRepository::get_list( (array) $arr_search );
        $arr_list->appends($arr_search);//添加链接参数
        $arr_list = list_page_data( $arr_list , $arr_search);
        foreach ( $arr_list['data'] as $key => &$value){
            $value['times'] = date('Y-m-d H:i:s' , $value['start_time']) .' 至 ' .date('Y-m-d H:i:s' , $value['over_time']);
            $temp = '';
            foreach ( explode( ',', $value['type'] ) as $k => $var) {
                $temp .= $notice_types[$var].',';
            }
            $value['type_msg'] = rtrim($temp,',');
        }
        return hello_success('成功',$arr_list);
    }

    /**
     *  获取指定的公告
     *  hinq 2019 08 21
     *
     **/
    public function getOne( int $id ){
        $arr_info = NoticeRepository::get_one( $id );
        $arr_info['times'] = date('Y-m-d H:i:s',$arr_info->start_time).' 至 '. date('Y-m-d H:i:s',$arr_info->over_time);
        $types = explode(',', $arr_info['type']);
        $arr_info['types'] = $types;
        return $arr_info;
    }


    /**
     *  添加公告
     *  hinq  2019 08 21
     *
     **/
    public function addOne( array $arrInput ){
        try {

            if( empty( $arrInput['title'] ) ){
                return hello_error('请设置公告标题');
            }
            if( empty( $arrInput['content'] ) ){
                return hello_error('请设置公告内容');
            }
            if( empty( $arrInput['times'] ) ){
                return hello_error('请设置公告的时间');
            }
            if( count( $arrInput['type'] ) <= 0 ){
                return hello_error('请选择公告类型');
            }
            $types = implode(',', $arrInput['type']);
            //添加
            $arrInput['type'] = $types;
            $arrInput['author_id'] = admin_user_id();

            $times = explode(' 至 ',$arrInput['times'] );
            unset( $arrInput['times'] );
            $arrInput['start_time'] = strtotime( $times[0] );
            $arrInput['over_time'] = strtotime( $times[1] );

            $arrInput['sort'] = 20;
            // $arrInput['type'] = 1;
            $arrInput['status'] = 1;
            unset( $arrInput['_token'] );
            $res = NoticeRepository::add_one( (array) $arrInput);
            if (empty($res)) {
                return hello_error('添加失败');
            }

            return hello_success('添加成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  编辑公告数据
     *  hinq 2019 08 21
     *  
     **/
    public function edit_one( array $arrInput ){
        try {

            if( empty( $arrInput['title'] ) ){
                return hello_error('请设置公告标题');
            }
            if( empty( $arrInput['content'] ) ){
                return hello_error('请设置公告内容');
            }
            if( empty( $arrInput['times'] ) ){
                return hello_error('请设置公告的时间');
            }
            // if( count( $arrInput['types'] ) <= 0 ){
            //     return hello_error('请选择公告类型');
            // }

            //编辑
            $times = explode(' 至 ',$arrInput['times'] );
            unset( $arrInput['times'] );
            unset( $arrInput['_token'] );
            $arrInput['start_time'] = strtotime( $times[0] );
            $arrInput['over_time'] = strtotime( $times[1] );
            $arrInput['type'] = trim( implode(',',$arrInput['type']) );

            $res = NoticeRepository::edit_one($arrInput);
            if (empty($res)) {
                return hello_error('编辑失败');
            }

            return hello_success('编辑成功', $res);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


    /**
     *  删除公告
     *  hinq 2019 09 10
     *
     **/
    public function del_one( int $nId ){
        try {
            if ($nId <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = NoticeRepository::delete_one($nId);
            if ($res === false) {
                return hello_error('删除失败');
            }
            return hello_success('删除成功');
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }



}